//菜单编辑
var groupid;
var iconid;
var subid;
var menuid;
var id;
var clientType = 0;
$(function() {
  id = ly.getUrlParam("id");
  groupid = ly.getUrlParam("groupid");
  iconid = ly.getUrlParam("iconid");
  subid = ly.getUrlParam("subid");
  menuid = ly.getUrlParam("menuid");
  $("#isUsing").getSelect({
    type: "radio",
    asName: "isUsing",
    list: [
      { value: "0", display: "禁用" },
      { value: "1", display: "启用" }
    ],
    default: 1
  });

  if (id == null) {
    id = '';
  }
  $("#parentId").getColumn({
    id: id,
    "thisUrl": site.apiUrl + '/back/menu/selectMenuAllById?id=' + id
  }, function(data) {
    console.log(data);
    var code = data.code;
    var level = "";
    clientType = data.clientType;
    $("#level").html();
    if (data.name != '根目录') {
      level = parseInt(code.length / 3 + 1);
    } else {
      level = 0;
    }
    $("#level").html(level);
    var url = site.apiUrl + "/back/menu/selectMenuCreatCodeById";
    requestPost(url, { 'id': data.id, 'level': level }, changeThisMenuCode, 'POST', false, false);
  });
  //保存
  $("body").on("click", "#save_btn_menu", function(event) {
    event.preventDefault();
    //var obj = education_packageObj();
    var success = $('#form').validate('submitValidate');
    if (success) {
      save();
    }

  });

  iconSelector();

});

function changeThisMenuCode(data) {
  console.log(data);
  if (data.code == 200) {
    $("#menuCode").val(data.data);
    //客户端类型
    $("#clientTypeAll").removeClass("hide");
    // $("#clientType").getSelect({
    //   type: "select",
    //   asName: "clientType",
    //   code: "roleAffiliate",
    //   list: [{
    //     "value": '',
    //     "display": '请选择'
    //   }],
    //   default: clientType
    // });
  }
}

function save() {
  var obj = packageObj();
  var url = site.apiUrl + "/back/menu/insertMenu";
  //ajaxForm(url,obj,submitCallback,'POST',false,false);

  request({
    url: url,
    data: obj,
    type: "post",
    ajaxform: 'true',
    callback: function(f) {
      console.log(f);
      if (f.code == 200) {
        ly.alert('创建成功', function() {
          var k = 0;
          $("#save_btn_menu").attr("disabled", "disabled");
          ly.confirm("是否继续创建？", function() {
            ly.goto("menu-create.html?id=" + id + "&groupid=" + groupid + "&iconid=" + iconid + "&subid=" + subid + "&menuid=" + menuid);
          }, function() {
            ly.goto("menu.html?groupid=" + groupid + "&iconid=" + iconid + "&subid=" + subid + "&menuid=" + menuid);
          });
        });
      } else {
        ly.toast(f.message);
      }
    }
  });
}


function submitCallback(data) {
  console.log(data);
  if (data.code == 200) {
    ly.alert('创建成功', function() {
      var k = 0;
      $("#save_btn_menu").attr("disabled", "disabled");
      ly.confirm("是否继续创建？", function() {
        ly.goto("menu-create.html?id=" + id + "&groupid=" + groupid + "&iconid=" + iconid + "&subid=" + subid + "&menuid=" + menuid);
      }, function() {
        ly.goto("menu.html?groupid=" + groupid + "&iconid=" + iconid + "&subid=" + subid + "&menuid=" + menuid);
      });
    });

  } else {
    ly.toast(data.message);
  }
}
//数据打包
function packageObj() {
  var isUsing = $("#isUsing input:checked").val(); //当前状态
  var lmtype3 = $("input[name='lmtype3']").attr("data-id"); //父级菜单
  var level = $("#level").html(); //层级
  var menuCode = $("#menuCode").val(); //菜单编码
  var icon = $("#icon").val(); //图标
  var menuName = $("#menuName").val(); //菜单标题
  var url = $("#url").val(); //菜单URL
  var nodeOrder = $("#nodeOrder").val(); //顺序
  // var clientType = $('select[name=clientType] option:checked').val(); //客户端类型
  var createUserId = window.sessionStorage.getItem("backstage-access-id");
  //var background = $(".minicolors-swatch-color").attr("style").split(":")[1];
  var background = $("#background").val();
  if (id == null && id == '') {
    id = "0";
  }
  /* var f = {
    	'fileName':fileName,
    	'filePath':filePath,
    	//'fileType':fileType,
    	'fileSize':fileSize
   };*/
  var obj = {
    'isUsing': isUsing,
    'parentId': lmtype3 != null && lmtype3 != '' ? lmtype3 : 0,
    'level': level != null && level != '' ? level : 0,
    'menuCode': menuCode,
    'menuName': menuName,
    'url': url,
    'nodeOrder': nodeOrder,
    'background': background,
    'createUserId': createUserId,
    // 'clientType': 'selectinputnameclientTypest',
    'icon': icon
    //'parentId':id
  };

  console.log(obj);
  return obj;
}